import $ from '../common';
Page({
  data: {
    arr: [{}, {}, {}],
    currMonthIndex: 0, //swiper展示的第几个
    titleHeight: $.gData().titleHeight,
    statuHeight: $.gData().statuHeight,
    week: ['一', '二', '三', '四', '五', '六', '日'],
    clickinfo: '', //记录点击的时间，然后通过每一项时间判断点击的哪一个是那一个，然后添加样式  排他
    isfrist: false, //一开始默认选择的时候今日的日期，点击不存在，如果选择其他时间，点击就会存在，如果点击对应的时间第点击将它变成不存在，然后更改默认选择的样式
    lessinfo: [],
    count: 0,
    fristload: true,
  },
  onLoad(e) {
    let date = new Date() //获取时间
    let year = date.getFullYear() //获取年
    let month = date.getMonth() //获取月

    let a = month + 1
    let months = a > 10 ? a : '0' + a
    let day = date.getDate()

    let today = year + '-' + months + '-' + day //今天的时间 格式是2022-04-20
    this.setData({
      years: year,
      months: a,
      today,
    })
    this.getinfo(year, month + 1) //本月
    this.getinfo(year, month + 2, 1) //下月
    this.getinfo(year, month, 2) //上月
  },
  itemdayclick(e) {
    let {
      today,
      isfrist,
      lessinfo
    } = this.data
    if (e.currentTarget.dataset.clickinfo != today) {
      isfrist = true
    } else {
      isfrist = false
    }
    this.setData({
      clickinfo: e.currentTarget.dataset.clickinfo,
      lessinfo: e.currentTarget.dataset.clicklesson,
      isfrist,
    })
  },
  clickhand(e) { //点名的点击事件
    const {lesson_uuid,sign_status} = e.currentTarget.dataset 
    $.navigateTo(`/Signin/Signin?lesson_uuid=${lesson_uuid}&sign_status=${sign_status}`)
  },
  getinfo(year, month, index = 0) { //获取日期
    // let date = new Date(year, month+1, 0)  
    // console.log(date.getFullYear(),date.getMonth(),index);
    let {
      months,
      years,
      today,
      lessinfo
    } = this.data
    if (month > 12) { //因为上面注释的内容不是很理想
      year++
      let time = month - 12
      month = time
    } else if (month < 1) {
      if (month == 0) {
        month = 12
      } else if (month == -1) {
        month = 11
      }
      year--;
    }
    this.data.count++
    $.get('/teacher/sign/list', {
      year,
      month,
      status: ''
    }, res => {
      this.data.count--
      res.forEach(v => {
        v.forEach(a => {
          if (a.lesson.length > 0) {
            a.current_month == "NO" ? a.color = '#e9cac6' : a.color = '#c5666a'
          } else {
            a.current_month == "NO" ? a.color = '#e0e0e0' : a.color = '#9e9e9e'
          }
          if (months == month && years == year && this.data.fristload) {
            if (today == a.dateseq) {
              lessinfo = a.lesson
              this.setData({
                lessinfo,
                fristload: false
              })
            }
          }
          a.itemday = a.dateseq.split('-')[2]
        })
      });
      this.data.arr[index].year = year
      this.data.arr[index].month = month
      this.data.arr[index].days = res
      this.setData({
        arr: this.data.arr,
      })
    })
  },
  leftclick() { //左边点击
    const {
      currMonthIndex
    } = this.data
    let a = currMonthIndex
    a === 0 ? a = 2 : a--
    this.setData({
      currMonthIndex: a
    })
  },
  rightclick() { //右边点击
    const {
      currMonthIndex
    } = this.data
    let a = currMonthIndex
    a === 2 ? a = 0 : a++
    this.setData({
      currMonthIndex: a
    })
  },
  calendarSwiper(e) { //swiper滑动触发
    let currMonthIndex = e.detail.current
    let thisMonth = this.data.arr[currMonthIndex]
    let {
      month,
      year
    } = thisMonth
    if (currMonthIndex === 0) {
      this.getinfo(year, month + 1, 1)
      this.getinfo(year, month - 1, 2)
    } else if (currMonthIndex === 1) {
      this.getinfo(year, month + 1, 2)
      this.getinfo(year, month - 1, 0)
    } else if (currMonthIndex === 2) {
      this.getinfo(year, month + 1, 0)
      this.getinfo(year, month - 1, 1)
    }
    this.setData({
      currMonthIndex
    })
  },
  onShow() {}
})